using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using Kiiro.Common.Utility;
using SecurityConstants = Kiiro.Common.Utility.Constants.Security;

namespace Kiiro.Common.Entities
{
    public class LicensingManifest
    {
        public double OpenProjectsLimit
        {
            get;
            set;
        }

        public double ActiveUsersLimit
        {
            get;
            set;
        }

        public DateTime ExpiryDate
        {
            get;
            set;
        }

        public string InstallCode
        {
            get;
            set;
        }

        public bool IsServerLicense
        {
            get;
            set;
        }



        /// <summary>
        /// Returns a comma delimited string of licensing parameters.
        /// </summary>
        /// <returns></returns>
        /// <see cref="http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.universalsortabledatetimepattern(VS.85).aspx"/>
        public string GetManifestCSV ( )
        {
            StringBuilder builder = new StringBuilder ( );
            builder.Append ( "OpenProjectsLimit|" + this.OpenProjectsLimit + "," );
            builder.Append ( "ActiveUsersLimit|" + this.ActiveUsersLimit + "," );
            // universal date time format: "yyyy'-'MM'-'dd HH':'mm':'ss'Z'"
            builder.Append ( "ExpiryDate|" + this.ExpiryDate.ToUniversalTime ( ).ToString ( System.Globalization.DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern ) + "," );
            builder.Append ( "InstallCode|" + this.InstallCode + "," );
            builder.Append ( "LicenseType|" + ( IsServerLicense ? "Server" : "Instance" ) );

            return builder.ToString ( );
        }

        /// <summary>
        /// Accepts a comma delimited list of licensing parameters and populates object properties.
        /// </summary>
        /// <param name="manifestCSV"></param>
        public void SetCSVToManifest ( string manifestCSV )
        {
            string [ ] manifestItems = manifestCSV.Split ( ',' );

            foreach ( string item in manifestItems )
            {
                string [ ] elements = item.Split ( '|' );

                if ( elements.Length == 2 )
                {
                    if ( elements [ 0 ] == "OpenProjectsLimit" )
                    {
                        try
                        {
                            this.OpenProjectsLimit = Double.Parse ( elements [ 1 ] );
                        }
                        catch { this.OpenProjectsLimit = 0; }
                    }
                    else if ( elements [ 0 ] == "ActiveUsersLimit" )
                    {                        
                        try
                        {
                            this.ActiveUsersLimit = Double.Parse ( elements [ 1 ] );
                        }
                        catch { this.ActiveUsersLimit = 0; }
                    }
                    else if ( elements [ 0 ] == "ExpiryDate" )
                    {
                        try
                        {
                            this.ExpiryDate = Convert.ToDateTime ( elements [ 1 ] );
                        }
                        catch { this.ExpiryDate = DateTime.MinValue; }
                    }
                    else if ( elements [ 0 ] == "InstallCode" )
                    {
                        try
                        {
                            this.InstallCode = elements [ 1 ];
                        }
                        catch { this.InstallCode = default ( string ); }
                    }
                    else if ( elements [ 0 ] == "LicenseType" )
                    {
                        try
                        {
                            IsServerLicense = elements [ 1 ] == "Server";
                        }
                        catch { this.IsServerLicense = false; }
                    }
                }
            }
        }

        /// <summary>
        /// Decrypts the encrypted license key string and returns a manifest object.
        /// </summary>
        /// <param name="encryptedLicenseKey"></param>
        /// <param name="validKey"></param>
        /// <returns></returns>
        public static LicensingManifest GetLicensingManifest ( string encryptedLicenseKey )
        {
            LicensingManifest manifest = null;

            try
            {
                string decryptedLicenseKey = KiiroEncryption.Instance.TripleDESDecrypt( encryptedLicenseKey );

                manifest = new LicensingManifest ( );
                manifest.SetCSVToManifest ( decryptedLicenseKey );
            }
            catch
            {
                manifest = null;
            }

            return manifest;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Common/Entities/LicensingManifest.cs $
 * 
 * 8     8/10/09 6:41p Camerons
 * Updates for Server License
 * 
 * 7     28/09/09 10:13a Camerons
 * 
 * 6     17/08/09 10:38a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/